using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

public enum DialogueMode
{
    Normal,
    Min
}

public class Dialogue : MonoBehaviour
{
    public static Dialogue Instance;
    public GameObject[] normalRoots;
    public Text textDes;
    public Image imageItem;
    public Text textItemCount;
    public GameObject taskFinishRoot;
    public DialogueMode mode = DialogueMode.Normal;
    [SerializeField]
    private Animation _taskFinishAnimation;
    // Start is called before the first frame update
    private void Awake()
    {
        Instance = this;
        Hide();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Hide()
    {
        gameObject.SetActive(false);
    }
    private int _taskId;
    public void Show(int taskId)
    {

        //初始化任务
        var taskItem = TaskMgr.Instance.GetTaskItemById(taskId);
        if (taskItem.IsFinish())
        {
            Debug.Log("如果任务完成了，不再显示对话框");
            return;
        }
       
        _taskId = taskId;


        gameObject.SetActive(true);

        //显示文字
        var configTask = ConfigTask.GetElement(_taskId);
        textDes.text = configTask.Des + "/" + configTask.DesFinish;

        //显示道具
        if (configTask.Type == TaskType.Item)
        {
            //道具
            var configItem = ConfigItem.GetElement(configTask.Key);
            XRes.LoadAsset<Sprite>(ConfigItemHelper.GetSpriteName(configItem), (sprite) =>
            {
                imageItem.sprite = sprite;
                imageItem.SetNativeSize();
                XRes.ReleaseAsset(sprite);
            });
            //数量
            textItemCount.text = ItemMgr.Instance.GetItemCount(configTask.Key) + "/" + configTask.Value;
        }
        else
        {
            Debug.LogError("unhandle configTask.Type:" + configTask.Type);
        }

        //是否可以交任务
        var isCanFinish = taskItem.IsCanFinish();
        taskFinishRoot.SetActive(isCanFinish);
        if (isCanFinish)
            UtilAnimation.PlayButtonAnimation(_taskFinishAnimation);
        else
            UtilAnimation.StopButtonAnimation(_taskFinishAnimation);
    }

    public void OnCloseToggleClick(bool isOn)
    {
        if(isOn)
        {
            mode = DialogueMode.Min;
        }
        else
        {
            mode = DialogueMode.Normal;
        }
        for (int i = 0; i < normalRoots.Length; i++)
        {
            normalRoots[i].SetActive(mode == DialogueMode.Normal);
        }
    }

    public void OnTaskFinishbuttonClick()
    {
        Debug.Log("item count:" + ItemMgr.Instance.GetItemCount(ItemDef.TypeIdWater));
        //#error 要写 道具增加系统
        //#error 要写 用户按下按钮，采摘道具
        //#error 要写 Tips系统

        //检测任务是否完成
        var taskItem = TaskMgr.Instance.GetTaskItemById(_taskId);
        if (taskItem.IsCanFinish())
        {
            UtilAnimation.StopButtonAnimation(_taskFinishAnimation);
            taskItem.ExeFinish();
            PassDetector.Instance.SetCanPass(taskItem.gridX, taskItem.gridY);
            Hide();
        }
    }
}